package com.zytlifeservice.controller.pay;

import com.zytlifeservice.domain.Result;
import com.zytlifeservice.exception.UserException;
import com.zytlifeservice.response.pay.QueryInfo;
import com.zytlifeservice.service.pay.WaterCoalService;
import com.zytlifeservice.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by MJS on 2017/6/1.
 */
@RestController
@RequestMapping(value = "/waterCoal")
public class WaterCoalController {

    private static final Logger logger = LoggerFactory.getLogger(WaterCoalController.class);

    @Autowired
    WaterCoalService waterCoalService;


    @PostMapping(value = "/queryInfoByAddress")
    public Result queryByCity(@RequestParam("address") String address,
                              @RequestParam("type") int type) {
        return ResultUtil.success(waterCoalService.queryInfoByAddress(address, type), "查询成功");
    }


    @PostMapping(value = "/queryAccountInfo")
    public Result queryAccountInfo(@RequestParam("itemId") String itemId,
                                   @RequestParam("account") String account) {
        return ResultUtil.success(waterCoalService.queryItemInfo(itemId, account), "查询成功");
    }

    @PostMapping(value = "/preCreate")
    public Result preCreate(@RequestParam("itemId") String itemId,
                            @RequestParam("itemName") String itemName,
                            @RequestParam("address") String address,
                            @RequestParam("rechargeAmount") String rechargeAmount,
                            @RequestParam("account") String account,
                            @RequestParam("type") int type,
                            @RequestParam("userId") Long userId) {
        //这里实际上是下订单的过程
        String[] result = waterCoalService.preCreateOrder(itemId, itemName, address, rechargeAmount, account, type, userId);
        if ("success".equals(result[0])) {
            return ResultUtil.success(result[1], "预下单成功");
        } else {
            throw new UserException(result[1], 202);
        }
    }


    @PostMapping(value = "/wxPreCreate")
    public Result wxPreCreate(@RequestParam("itemId") String itemId,
                              @RequestParam("itemName") String itemName,
                              @RequestParam("address") String address,
                              @RequestParam("rechargeAmount") String rechargeAmount,
                              @RequestParam("account") String account,
                              @RequestParam("type") int type,
                              @RequestParam("userId") Long userId,
                              @RequestParam("ip") String ip) {

        String[] result = waterCoalService.wxPreCreate(itemId, itemName, address, rechargeAmount, account, type, userId, ip);
        if ("success".equals(result[0])) {
            return ResultUtil.success(result[1], "预下单成功");
        } else {
            throw new UserException(result[1], 202);
        }
    }

    @PostMapping(value = "/testQueryAccount")
    public Result testAccount(@RequestParam("itemId") String itemId,
                              @RequestParam("account") String account,
                              @RequestParam("projectId") String projectId,
                              @RequestParam("unitId") String unitId,
                              @RequestParam("province") String province,
                              @RequestParam("city") String city,
                              @RequestParam("unitName") String unitName,
                              @RequestParam("cityId") String cityId,
                              @RequestParam("modeId") String modeId
    ) {

        QueryInfo queryInfo = new QueryInfo(itemId, account, projectId, unitId, province, city, unitName, cityId, modeId);
        logger.info("test QueryInfo ={}", queryInfo);
        return ResultUtil.success(waterCoalService.queryAccountInfo(queryInfo), "查询成功");
    }

}
